package network.okhttp.getAsynchronous7_3;

import okhttp3.*;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;

/**
 * 异步请求爬取http://www.17173.com内容
 */
public class PageOperator {
    //定义一个OKHttpClient
    private OkHttpClient okHttpClient;
    public PageOperator(){
        okHttpClient = new OkHttpClient();
    }
    public String getPageContentAsync(String url){
        if (StringUtils.isBlank(url)){
            return null;
        }
        //定义一个request
        String result=null;
        Request request = new Request.Builder().url(url).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {

            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String result = response.body().string();
                System.out.println("call "+url+", content's size is " + result.length());
                System.out.println(result);

            }
        });
        return "success";
    }

    public static void main(String[] args) {

        System.out.println(new PageOperator().getPageContentAsync("http://www.houqinbao.com/hydropower/index.php?m=PayWeChat&c=Index&a=before&payopenid=&openid=oUiRowVf9LEm212UhpyCfXVjl4GM&schoolcode=13579&campusid=13579_173&areaid=13579_173_288&flatid=13579_173_288_949&roomname=A7092"));

    }
}
